﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ECare.BUS.Data;
using ECare.BUS;

using ECare.Client.templates;
//
namespace ECare.Client.frms
{
    public partial class frmIncs : frmGridTemplate
    {
        public frmIncs()
        {
            InitializeComponent();

            this.Load += frmIncs_Load;

            this.grd.CellFormatting += grd_CellFormatting;

            this.grd.DataError += grd_DataError;
        }

        void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grd.Rows.Count)
            {
                if (grd.Columns[e.ColumnIndex].Name == "supply_checkstatus")
                {
                    var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                    if (currentstatus == CheckStatus.Qualitified.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (currentstatus == CheckStatus.Rejected.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (currentstatus == CheckStatus.UnFinished.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {

                    }
                }
            }
        }


        public BindingList<SupplyInfo> source { get; set; }

        void frmIncs_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            resource_id_column.DataSource = ResourcesHelper.GetAllResources();
            resource_id_column.DisplayMember = "ResourceName";
            resource_id_column.ValueMember = "ResourceId";


            supply_create_by.DataSource = UserInfoHelper.GetUsers();
            supply_create_by.DisplayMember = "UserName";
            supply_create_by.ValueMember = "UserId";


            supply_update_by.DataSource = UserInfoHelper.GetUsers();
            supply_update_by.DisplayMember = "UserName";
            supply_update_by.ValueMember = "UserId";




            if (!CheckPermission(1))
                this.btnAdd.Enabled = false;

            if (!CheckPermission(2))
                this.btnEdit.Enabled = false;

            this.LoadData("1=1");
        }

        private void LoadData(string where)
        {
            this.source = new BindingList<SupplyInfo>(SupplyInfoHelper.GetAllSupplyInfo(where));
            this.grd.DataSource = new BindingSource(this.source, null);
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmInInfo();
            frm.entity = null;
            frm.Saved += frm_Saved;
            frm.ShowDialog();

        }

        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<SupplyInfo>();
            var role = entity as SupplyInfo;
            var findresult = source.FirstOrDefault(a => a.supply_id == role.supply_id);
            if (findresult == null)
                this.source.Add(role);
        }

        private void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex == -1)
                return;

            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as SupplyInfo;
            var frm = new frmInInfoReadonly();
            frm.CurrentSupply = entitye;
            frm.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {


            if (this.grd.SelectedRows.Count == 0)
                return;
            var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;

            if (entitye.supply_checkstatus == CheckStatus.Qualitified)
            {
                MessageBox.Show("当前订单已经被质检完成,不能被修改!");
                return;
            }


            var frm = new frmInInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            var frm = new frmSupplyQuery();
            frm.SearchCompleted += frm_SearchCompleted;
            frm.ShowDialog();
        }

        void frm_SearchCompleted(string where)
        {
            //throw new NotImplementedException();

            this.LoadData(where);
        }
    }
}
